package com.party.controller;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.party.domain.Blurb;

import com.party.domain.Img;
import com.party.domain.Product;
import com.party.domain.User;
import com.party.dto.Response;
import com.party.service.impl.ProductServiceImpl;



@RestController
@RequestMapping("/product")
public class ProductController
{
	final Logger logger = LoggerFactory.getLogger(ProductController.class);
	@Autowired
	private	ProductServiceImpl prservice;
	
	@Autowired
	HttpServletResponse response;
	
	@Autowired
	HttpServletRequest request;
	
	//增加商品
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public Response addProduct(String type,String validTime,String city,String description,String title,String value,     String address,String numberPeople,String numberBed,String numberRoom,String supportingFacility,String blurbLabel,String supplement,String warmPrompt,String btype,   @RequestParam("file") MultipartFile[] files,HttpServletRequest request)
	{
		
		try {
			logger.info("增加商品中" + title);
			int productId=prservice.addProduct(type, validTime, city, description, title, value);
			logger.info("增加商品成功" + title);
			
			logger.info("增加商品详情中" + title);
			prservice.addBlurb(productId, address, numberPeople, numberBed, numberRoom, supportingFacility, blurbLabel, supplement, warmPrompt,btype);
			logger.info("增加商品详情成功" + title);
			

			for(int i = 0;i<files.length;i++){  
				System.out.println("fileName---------->" + files[i].getOriginalFilename());  
				String newfilename="D://" + new Date().getTime() + files[i].getOriginalFilename();
				if(!files[i].isEmpty()){  
					int pre = (int) System.currentTimeMillis();  
					
					try {  
						//拿到输出流，同时重命名上传的文件  

						FileOutputStream os = new FileOutputStream(newfilename);  
						//拿到上传文件的输入流  
						FileInputStream in = (FileInputStream) files[i].getInputStream();  
						  
						//以写字节的方式写文件  
						int b = 0;  
						while((b=in.read()) != -1){  
							os.write(b);  
						}  
						os.flush();  
						os.close();  
						in.close();  
						int finaltime = (int) System.currentTimeMillis();  
						System.out.println(finaltime - pre);  
						  
					} catch (Exception e) {  
						e.printStackTrace();  
						System.out.println("上传出错");  
					}  
			
				} 
				logger.info("增加商品图片中" + title);
				prservice.addImage(productId, newfilename);
				logger.info("增加商品图片成功" + title);
			}
			
			
			
			return Response.success();
		} catch (Exception e) {
			logger.error("增加商品出错", e);
			return Response.failure();
		}
			
	}
	
	//按id删除商品
	@RequestMapping(value="/delete")
	public Response deleteProduct(int pid)
	{
		try {
			logger.info("删除商品图片中" + pid);
			prservice.deleteImage(pid);
			logger.info("删除商品图片成功" + pid);
			
			logger.info("删除商品详情中" + pid);
			prservice.deleteBlurb(pid);
			logger.info("删除商品详情成功" + pid);
			
			logger.info("删除商品中" + pid);
			prservice.deleteProduct(pid);
			logger.info("删除商品成功" + pid);
			
			return Response.success();
		} catch (Exception e) {
			
			logger.error("删除商品出错", e);
			return Response.failure();
		}
	}
	
	//按分类查询商品
	@RequestMapping(value="findtype")
	public Response findType(String type,int page,int size)
	{
		try {
			List<Product> pro=prservice.findTypeProduct(type, page, size);
			return Response.success(pro);
			
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	
	//按二级分类查询商品
	@RequestMapping(value="findBlurbType")
	public Response findBlurbType(String type,int page,int size)
	{
		try {
			List<Product> pro=prservice.FindBlurbTypeProduct(type, page, size);
			return Response.success(pro);
			
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	
	//按id获取商品
	@RequestMapping(value="/getproduct")
	public Response getProduct(int pid)
	{
		try {
			logger.info("ID获取商品详情中：" + pid);
			Product pro=prservice.getIdProduct(pid);
			logger.info("ID获取商品成功：" + pid);
			return Response.success(pro);
		} catch (Exception e) {
			logger.info("ID获取商品失败：" + pid);
			return Response.failure();
		}
	}
	
	
	
	//按id获取商品详情
	@RequestMapping(value="/getblurb")
	public Response getBlurb(int pid)
	{
		try {
			logger.info("ID获取商品详情中：" + pid);
			Blurb blurb=prservice.getIdBlurb(pid);
			logger.info("ID获取商品成功：" + pid);
			return Response.success(blurb);
		} catch (Exception e) {
			logger.info("ID获取商品失败：" + pid);
			return Response.failure();
		}
	}
	
	
	//按id获取商品图片
	@RequestMapping(value="/getimg")
	public Response getImg(int pid)
	{
		try {
			logger.info("ID获取商品图片中：" + pid);
			List<Img> img=prservice.getIdImg(pid);
			logger.info("ID获取商品图片成功：" + pid);
			return Response.success(img);
		} catch (Exception e) {
			logger.info("ID获取商品图片失败：" + pid);
			return Response.failure();
		}
	}
	
	
	

	//按id获取商品有关所有数据
	@RequestMapping(value="/getcom")
	public Response getCommodity(int pid)
	{
		
		try {			
			Product pro=prservice.getIdProduct(pid);
			Blurb blurb=prservice.getIdBlurb(pid);		
			List<Img> img=prservice.getIdImg(pid);
			List<Object> com = new ArrayList<Object>(); 
			com.add(pro);
			com.add(blurb);
			com.add(img);			
			return Response.success(com);
		} catch (Exception e) {
			return Response.failure();
		}
		
	}
	
	
	//更新商品数据
	@RequestMapping(value="upProduct")
	public Response upProduct(Product pro)
	{
		try {
			System.out.println(pro.getPid());
			prservice.updateIdProduct(pro);
			return Response.success();
		} catch (Exception e) {
			return Response.failure("数据更新失败");
		}
	}
	
	
	//更新商品详情数据
	@RequestMapping(value="upBlurb")
	public Response upBlurb(Blurb blu)
	{
		try {	
			prservice.updateIdBluct(blu);
			return Response.success();
		} catch (Exception e) {
			return Response.failure("数据更新失败");
		}
	}
	
	
	
	//更新商品图片
	@RequestMapping(value="upimg")
	public Response upImg(int pid,@RequestParam("files") MultipartFile[] files,HttpServletRequest request)
	{
		
		try {
			logger.info("删除商品图片中" + pid);
			prservice.deleteImage(pid);
			logger.info("删除商品图片成功" + pid);
			
			
			for(int i = 0;i<files.length;i++){  
				System.out.println("fileName---------->" + files[i].getOriginalFilename());  
				String newfilename="D://" + new Date().getTime() + files[i].getOriginalFilename();
				if(!files[i].isEmpty()){  
					int pre = (int) System.currentTimeMillis();  
					
					try {  
						//拿到输出流，同时重命名上传的文件  

						FileOutputStream os = new FileOutputStream(newfilename);  
						//拿到上传文件的输入流  
						FileInputStream in = (FileInputStream) files[i].getInputStream();  
						  
						//以写字节的方式写文件  
						int b = 0;  
						while((b=in.read()) != -1){  
							os.write(b);  
						}  
						os.flush();  
						os.close();  
						in.close();  
						int finaltime = (int) System.currentTimeMillis();  
						System.out.println(finaltime - pre);  
						  
					} catch (Exception e) {  
						e.printStackTrace();  
						System.out.println("上传出错");  
					}  
			
				} 
				logger.info("增加商品图片中" );
				prservice.addImage(pid, newfilename);
				logger.info("增加商品图片成功");
			}
			
			
			return Response.success();
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	
	@RequestMapping(value="/ceshi")
	public Response ceshia(String type)
	{
		try {
			List<Product>pro=prservice.findTypeceshi(type);
			
			return Response.success(pro);
			
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	
	
	
}
